package com.iotimc.attendancesystem.manage.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.entity.AttendanceLogEntity;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.attendancesystem.util.PictureManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by cwj on 2017/11/6.
 *
 * @author cwj
 */

public class WorkManageAdapter extends RecyclerView.Adapter<WorkManageAdapter.ViewHolder> {
    private List<AttendanceLogEntity> info;
    private Context context;
    private LayoutInflater inflater;
    private String src;

    public WorkManageAdapter(List<AttendanceLogEntity> info, Context context) {
        this.info = info;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void addData(List<AttendanceLogEntity> info) {
        this.info = info;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_manage_recy_work, parent, false));
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AttendanceLogEntity attendanceLogEntity = info.get(position);
        PersonEntity personEntity = attendanceLogEntity.getPersonEntity();

        //holder.mIvManageWorkHead.setImageBitmap(ImageUtil.bytesToBitmap(attendanceLogEntity.getPersonEntity().getImage()));
        //PictureManager.getInstance().loadCirclePic(context, personEntity.getImage(), holder.mIvManageWorkHead, false);

        byte[] image = personEntity.getImage();
        if (image == null) {
            PictureManager.getInstance().loadCirclePic(context, R.drawable.ic_user_img, holder.mIvManageWorkHead, true);

        } else {
            PictureManager.getInstance().loadCirclePic(context, image, holder.mIvManageWorkHead, true);
        }

        String code = personEntity.getCode();
        if (!TextUtils.isEmpty(code)) {
            holder.mTvManageWorkCode.setText(code);
        }
        String name = personEntity.getName();
        if (!TextUtils.isEmpty(name)) {
            holder.mTvManageWorkName.setText(name);
        }
        String card = personEntity.getCard();
        if (!TextUtils.isEmpty(card)) {
            holder.mTvManageWorkCard.setText(card);
        }


        Date time = attendanceLogEntity.getCreateTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(time);

        holder.mTvManageWorkWork.setText(str);
        holder.mTvManageWorkSex.setText(info.get(position).getPersonEntity().getSex());
        if (position % 2 == 0) {
            holder.mLlManageWork.setBackground(context.getResources().getDrawable(R.drawable.bg_manage_list));
        } else {
            holder.mLlManageWork.setBackground(context.getResources().getDrawable(R.drawable.bg_manage_list2));
        }

    }

    @Override
    public int getItemCount() {
        return info.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mIvManageWorkHead;
        TextView mTvManageWorkCode;
        TextView mTvManageWorkName;
        TextView mTvManageWorkSex;
        TextView mTvManageWorkWork;
        TextView mTvManageWorkCard;
        LinearLayout mLlManageWork;

        ViewHolder(View itemView) {
            super(itemView);
            mIvManageWorkHead = itemView.findViewById(R.id.iv_manage_work_head);
            mTvManageWorkCard = itemView.findViewById(R.id.tv_manage_work_card);
            mTvManageWorkCode = itemView.findViewById(R.id.tv_manage_work_code);
            mTvManageWorkName = itemView.findViewById(R.id.tv_manage_work_name);
            mTvManageWorkSex = itemView.findViewById(R.id.tv_manage_work_sex);
            mTvManageWorkWork = itemView.findViewById(R.id.tv_manage_work_work);
            mLlManageWork = itemView.findViewById(R.id.ll_manage_work);
        }
    }
}
